package top.vains.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.sql.Types;

/**
 * 名称工具栏
 *
 * @author vains
 * @version 1.0
 * @since 2020/3/18 13:48
 **/
@Slf4j
public class TemplateUtils {

    private TemplateUtils() {}

    private static final String STRING = "String";

    private static final String DOUBLE = "Double";

    private static final String INTEGER = "Integer";

    private static final String BOOLEAN = "Boolean";

    private static final String DATE = "java.util.Date";

    private static final String BIG_DECIMAL = "java.math.BigDecimal";

    /**
     * 下划线转为驼峰
     * @param line 要转的字符串
     * @param firstIsUpperCase 是否首字母大写
     * @return 返回转化后的字符
     */
    public static String underLineCame(String line, boolean ... firstIsUpperCase) {
        String underLine = "_";
        String str = "";
        if(StringUtils.isBlank(line)){
            return str;
        } else {
            StringBuilder sb = new StringBuilder();
            String [] strArr;
            // 不包含下划线，且第二个参数是空的
            if(!line.contains(underLine) && firstIsUpperCase.length == 0){
                sb.append(line.substring(0, 1).toLowerCase()).append(line.substring(1));
                str = sb.toString();
            } else if (!line.contains(underLine) && firstIsUpperCase.length != 0){
                if (!firstIsUpperCase[0]) {
                    sb.append(line.substring(0, 1).toLowerCase()).append(line.substring(1));
                    str = sb.toString();
                } else {
                    sb.append(line.substring(0, 1).toUpperCase()).append(line.substring(1));
                    str = sb.toString();
                }
            } else if (line.contains(underLine) && firstIsUpperCase.length == 0) {
                strArr = line.split(underLine);
                for (String s : strArr) {
                    String upperCase = s.substring(0, 1).toUpperCase();
                    sb.append(upperCase).append(s.substring(1));
                }
                str = sb.toString();
                str = str.substring(0, 1).toLowerCase() + str.substring(1);
            } else if (line.contains(underLine) && firstIsUpperCase.length != 0) {
                strArr = line.split(underLine);
                for (String s : strArr) {
                    sb.append(s.substring(0, 1).toUpperCase()).append(s.substring(1));
                }
                if (!firstIsUpperCase[0]) {
                    str = sb.toString();
                    str = str.substring(0, 1).toLowerCase() + str.substring(1);
                } else {
                    str = sb.toString();
                }
            }
        }
        return str;
    }

    /**
     * 数据类型转化JAVA
     * @param sqlType：类型名称
     * @return 返回转换后的Java类型
     */
    public static String sqlToJava(Integer sqlType) {
        if( sqlType == null) {
            return null;
        }
        switch(sqlType){
            case Types.NVARCHAR:
                return STRING;
            case Types.CHAR:
                return "Char";
            case Types.VARCHAR:
                return STRING;
            case Types.NCHAR:
                return STRING;
            case Types.BLOB:
                return "byte[]";
            case Types.INTEGER:
                return INTEGER;
            case Types.TINYINT:
                return BOOLEAN;
            case Types.SMALLINT:
                return INTEGER;
            case Types.BIT:
                return BOOLEAN;
            case Types.BIGINT:
                return "java.math.BigInteger";
            case Types.FLOAT:
                return "Float";
            case Types.DOUBLE:
                return DOUBLE;
            case Types.DECIMAL:
                return BIG_DECIMAL;
            case Types.BOOLEAN:
                return BOOLEAN;
            case Types.ROWID:
                return "Long";
            case Types.DATE:
                return DATE;
            case Types.TIME:
                return "java.sql.Time";
            case Types.TIMESTAMP:
                return DATE;
            case Types.NUMERIC:
                return BIG_DECIMAL;
            case Types.REAL:
                return BIG_DECIMAL;
            default:
                log.error("未发现该类型：{}", sqlType);
                break;
        }
        return null;
    }

}
